package com.hamilton.erik.pt.mobile.android.volley;

import android.content.Context;
import android.util.Log;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.hamilton.erik.pt.mobile.android.PtApp;
import com.hamilton.erik.pt.mobile.android.net.ssl.PtCertSSLSocketFactory;
import com.hamilton.erik.pt.mobile.android.volley.toolbox.PtHostnameVerifyingHurlStack;
 
public class VolleySingleton {
    private static VolleySingleton instance = null;
    private RequestQueue requestQueue;
 
    private VolleySingleton(){
    	try {
        	Context context = PtApp.getAppContext();
        	
        	// Make sure to use an SSLSocketFactory that trusts our certificate(
        	// required for Android up to 4.1 to successfully execute SSL with
        	// self signed certs). Additionally make sure to use the custom
        	// HurlStack implementation that is configured to use a custom
        	// HostnameVerifier that will verify our server's hostname in order
        	// to successfully execute SSL with self signed certs for Android 4.1+).
			requestQueue =
					Volley.newRequestQueue(context, 
						new PtHostnameVerifyingHurlStack(null,
							PtCertSSLSocketFactory.getSSLSocketFactory(context)));
		} catch (Exception e) {
			Log.e("VolleySingleton", "Failed to initialize Volley Singleton: "
										+ e.getMessage());
		}
    }
 
    public static VolleySingleton getInstance(){
        if(instance == null){
            instance = new VolleySingleton();
        }
        return instance;
    }
 
    public RequestQueue getRequestQueue(){
        return this.requestQueue;
    }
 
}